package com.siqi.hr.system.common.service;

import com.siqi.hr.common.Constants;
import com.siqi.hr.common.redis.RedisAPI;
import com.siqi.hr.system.common.dao.intf.CatchDao;
import com.siqi.hr.system.common.model.ColumnUsage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取缓存数据Service层
 * @version 1.0.0, Sep 25, 2016
 * @author xiaogq
 * @since JDK1.6.0_12, Spring3.0.2
 */
@Service("catchService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class CatchService {

    @Autowired
    private CatchDao catchDao;

    //获取外键被那些表引用信息
    public void setColumnUsageCache(){
        List<ColumnUsage> culist = catchDao.getColumnUsageList();
        List reftable = new ArrayList();
        for (ColumnUsage usage : culist) {
            String refTName = usage.getTableName();
            if (!reftable.contains(refTName)) {
                JSONArray deArray = new JSONArray();
                for (ColumnUsage cu : culist) {
                    if (refTName.equals(cu.getReferencedTableName())) {
                        JSONObject obj = new JSONObject();
                        obj.put("tableName",cu.getTableName());
                        obj.put("columnName",cu.getColumnName());
                        deArray.add(obj);
                    }
                }
                RedisAPI.setKeyValue(refTName,deArray.toString(), Constants.DEFAULTDB_INDEX);
            }
        }
    }

}
